<?php
header("Content-type: text/html; charset=gbk");
include_once "HttpUtil.php";
include_once "Constant.php";
include_once "StageInfo.php";
include_once $_SERVER['DOCUMENT_ROOT']."/model/Task.php";
include_once $_SERVER['DOCUMENT_ROOT']."/model/Template.php";
include_once $_SERVER['DOCUMENT_ROOT']."/model/Stage.php";
include_once $_SERVER['DOCUMENT_ROOT']."/model/Location.php";
include_once $_SERVER['DOCUMENT_ROOT']."/dao/TaskDAO.php";
include_once $_SERVER['DOCUMENT_ROOT']."/dao/TemplateDAO.php";
include_once $_SERVER['DOCUMENT_ROOT']."/dao/StageDAO.php";
include_once $_SERVER['DOCUMENT_ROOT']."/dao/LocationDAO.php";
/**
 * Created by PhpStorm.
 * User: lwh
 * Date: 2017/7/27
 * Time: 14:12
 */
//接收post数据，并强制转换为关联数组
date_default_timezone_set('PRC');
$data = $_POST['data'];
$para = json_decode($data,true);
$result = getTaskInfo($para);
echo urldecode(json_encode($result));

function getTaskInfo($para){
    //输入参数
    $taskId = $para["taskid"];
    $latitude = $para["latitude"];
    $longitude = $para["longitude"];
    //返回结果集
    $response = new ResponseBO();
    //The DAO of task, Template and Stage
    $taskDAO = new TaskDAO();
    $templateDAO = new TemplateDAO();
    $stageDAO = new StageDAO();
    $locationDAO = new LocationDAO();
    /**
     * 获取task的相关信息
     */
    $task = $taskDAO->query($taskId);
    $response->setId($task->getId());
    $response->setTemplateId($task->getTemplateId());
    $response->setRequesterId($task->getUserId());
    $response->setTitle($task->getTitle());
    $response->setDescription($task->getDescription());
    $response->setStatus($task->getStatus());
    //设置进度
    $template = $templateDAO->query($task->getTemplateId());
    $response->setProgress($task->getCurrentStage()."/".$template->getTotalStageNum());
    $response->setCurrentStage($task->getCurrentStage());
    $response->setBonusReward($task->getBonusReward());
    $response->setPublishTime($task->getPublishTime());
    $response->setDeadline($task->getDeadline());
    //Set the stage info
    $stages = array();
    $originalStages = $stageDAO->queryStageListByTask($taskId);
    foreach ($originalStages as $originalStage){
        $stage =  StageInfo::getStageInfo($originalStage->getId(),-1,0);      
        if($task->getCurrentStage() == $originalStage->getIndex()){
            $currentDest = $locationDAO->queryByStageAndType($originalStage->getId(),Location::$TYPE_DEST);
            $time = calTime($longitude,$latitude,$currentDest);
            //计算contract:如果stage数量大于1，且不是最后一个stage，动态计算
            if($template->getTotalStageNum() > 1 && $originalStage->getIndex() != $template->getTotalStageNum()){
                $time = $time + Constant::$TIME_EXTRA_FOR_WORKER * 60;//额外预留时间，默认为10分钟
                $contractSec = time() + $time;
                $contract = date('Y-m-d H:i:s',$contractSec);
                //因为额外预留了十分钟，可能导致契约时间超过ddl，这里做一个比较和选择
                if($contractSec > strtotime($originalStage->getDeadline()))
                    $contract = $originalStage->getDeadline();
                $stage->setContractTime($contract);
			}
            //单个stage的任务，直接设定为ddl
            else
                $stage->setContractTime($task->getDeadline());
        }
        array_push($stages,$stage);
    }
    $response->setStages($stages);
    return $response;
}

//计算时间预估
function calTime($longitude,$latitude,$currentDest){
    $result = json_decode(HttpUtil::doGet($latitude,$longitude,$currentDest->getLatitude(),$currentDest->getLongitude()),true);
    if($result["status"] == 0){
        $result = $result["result"][0];
        $time = $result["duration"]["value"];
        return $time;
    }
    return -1;
}

class ResponseBO{
    public $id;
    public $templateId;
    public $requesterId;
    public $title;
    public $description;
    public $status;
    public $progress;
    public $currentStage;
    public $bonusReward;
    public $publishTime;
    public $deadline;
    public $stages;

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param mixed $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return mixed
     */
    public function getTemplateId()
    {
        return $this->templateId;
    }

    /**
     * @param mixed $templateId
     */
    public function setTemplateId($templateId)
    {
        $this->templateId = $templateId;
    }

    /**
     * @return mixed
     */
    public function getRequesterId()
    {
        return $this->requesterId;
    }

    /**
     * @param mixed $requesterId
     */
    public function setRequesterId($requesterId)
    {
        $this->requesterId = $requesterId;
    }

    /**
     * @return mixed
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * @param mixed $title
     */
    public function setTitle($title)
    {
        $this->title = urlencode($title);
    }

    /**
     * @return mixed
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * @param mixed $description
     */
    public function setDescription($description)
    {
        $this->description = urlencode($description);
    }

    /**
     * @return mixed
     */
    public function getStatus()
    {
        return $this->status;
    }

    /**
     * @param mixed $status
     */
    public function setStatus($status)
    {
        $this->status = $status;
    }

    /**
     * @return mixed
     */
    public function getProgress()
    {
        return $this->progress;
    }

    /**
     * @param mixed $progress
     */
    public function setProgress($progress)
    {
        $this->progress = $progress;
    }

    /**
     * @return mixed
     */
    public function getCurrentStage()
    {
        return $this->currentStage;
    }

    /**
     * @param mixed $currentStage
     */
    public function setCurrentStage($currentStage)
    {
        $this->currentStage = $currentStage;
    }

    /**
     * @return mixed
     */
    public function getBonusReward()
    {
        return $this->bonusReward;
    }

    /**
     * @param mixed $bonusReward
     */
    public function setBonusReward($bonusReward)
    {
        $this->bonusReward = $bonusReward;
    }

    /**
     * @return mixed
     */
    public function getPublishTime()
    {
        return $this->publishTime;
    }

    /**
     * @param mixed $publishTime
     */
    public function setPublishTime($publishTime)
    {
        $this->publishTime = $publishTime;
    }

    /**
     * @return mixed
     */
    public function getDeadline()
    {
        return $this->deadline;
    }

    /**
     * @param mixed $deadline
     */
    public function setDeadline($deadline)
    {
        $this->deadline = $deadline;
    }

    /**
     * @return mixed
     */
    public function getStages()
    {
        return $this->stages;
    }

    /**
     * @param mixed $stages
     */
    public function setStages($stages)
    {
        $this->stages = $stages;
    }

}


